function [ ] = qrFinder( img )
%% Preproces
if max(size(img)) > 666
    img = imresize(img, 666 / max(size(img)));
end

% Binarize
bimg = blockBinarization(img, 4);
bimg = logical(bimg);
% Add padding
bimg = padarray(bimg,[5 5],1);
bimg = bwareaopen(bimg,50);
bimg = bwmorph(bimg,'majority', 4);
%% Find boundaries
[B, ~, N, ~] = bwboundaries(bimg);
% Compute boundary properties for each boundary
components = Bounded.empty(length(B), 0);
for i = 1:length(B)
    components(i) = Bounded(B{i}, i, N);
end
%% Marker detection
    scale_area = 9;
    scale_diag = 3;
    scale_dist = 0.5;
    % Draw all the components
    count = 1;
    targets_big = Bounded.empty(3, 0);
    targets_small = Bounded.empty(3, 0);
    f = figure; imshow(bimg); hold on;
    for b = components
        if b.Hole && b.IsSquare
            % plot(b.Boundary(:,2), b.Boundary(:,1), 'w');
            for s = components
               % Filter square-ish markers
               if (b.Index ~= s.Index) && s.Hole && s.IsSquare ...
                       && b.AreaBox > s.AreaBox ...
                       && b.AreaBox < s.AreaBox * scale_area ...
                       && b.DiagonalBox < s.DiagonalBox * scale_diag ...
                       && b.pointInBox(s.CenterBox) ...
                       && s.pointInBox(b.CenterBox) ...
                       && pointDistance(b.CenterBox, s.CenterBox) < ...
                       s.DiagonalBox / scale_dist
                    plot(s.Boundary(:,2), s.Boundary(:,1), 'g', 'LineWidth', 1.5);
                    plot(b.Boundary(:,2), b.Boundary(:,1), 'g', 'LineWidth', 1.5);
                    plot(s.CenterBox(1), s.CenterBox(2), '.r', 'MarkerSize', 15);
                    if (count < 4)
                        targets_big(count) = b;
                        targets_small(count) = s;
                    end
                    count = count + 1;
               end
            end
        end
    end
if count == 4
    %% Calculate corners
    corners = zeros(3, 2);
    vectors = zeros(3, 2);
    for i = 1:1:3
        p = targets_big(i);
        y = targets_small(mod(i + 0, 3) + 1);
        z = targets_small(mod(i + 1, 3) + 1);
        c = (y.CenterBox + z.CenterBox) / 2;
        corner = c;
        dist = Inf;
        for j = 1:1:length(p.Boundary(:,1))
            b = [p.Boundary(j,2), p.Boundary(j,1)];
            d = pointDistance(b, c);
            if d < dist
                dist = d;
                corner = b;
            end
        end
        corners(i,:) = corner(:);
        v = p.CenterBox(:) - corner(:);
        vectors(i,:) = v / norm(v);
        plot(corner(1), corner(2), '.r', 'MarkerSize', 15);
        % plot([p(1), p(1) + vectors(i,1)], [p(2), p(2) + vectors(i,2)], '-r');
    end
    %% Calculate corners
    for i = 1:1:3
        j = mod(i + 0, 3) + 1;
        u = vectors(i,:);
        v = vectors(j,:);
        cu = targets_small(i).CenterBox;
        cv = targets_small(j).CenterBox;
        d = dot(u, v);
        if abs(d) < 0.5
            plot([corners(i,1), corners(j,1)], [corners(i,2), corners(j,2)], '-r', 'LineWidth', 2.5);
            plot([cu(1), cv(1)], [cu(2), cv(2)], '-r' , 'LineWidth', 2.5);
        end
    end
end
% set(gcf,'InvertHardCopy','off');
% saveas(f, int2str(f), 'bmp');
export_fig(int2str(f), '-png');
end

